package com.game.majiang.vo;

import java.util.ArrayList;
import java.util.Random;

import javax.sql.rowset.CachedRowSet;

import com.game.majiang.config.DataManager;
import com.game.server.db.SqlHelper;

import lombok.Data;

@Data
public class CrosstalkVo {

	public int id;
	public String content;
	public int usertype;
	public int tabletype;
	public int roomtype;
	
	
	public static ArrayList<CrosstalkVo> getListCrosstalk () {
		
		String sql = "SELECT * FROM crosstalk";
		ArrayList<CrosstalkVo> list = new ArrayList<CrosstalkVo>();
		CachedRowSet resultSet = SqlHelper.getResultSet(sql);
		try {
			while (resultSet.next()) {
				CrosstalkVo cv = new CrosstalkVo();
				cv.setId(resultSet.getInt("id"));
				cv.setContent(resultSet.getString("content"));
				cv.setRoomtype(resultSet.getInt("roomtype"));
				cv.setTabletype(resultSet.getInt("tabletype"));
				cv.setUsertype(resultSet.getInt("usertype"));
				list.add(cv);				
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
		
	}
	
	
	public static String getOneCrosstalk(int usertype, int tabletype, int roomtype) {
		
		ArrayList<CrosstalkVo> list = DataManager.getInstance().getCtv();
		ArrayList<CrosstalkVo> listS = new ArrayList<CrosstalkVo>();
		for (CrosstalkVo crosstalkVo : list) {
			if (crosstalkVo.getUsertype() == usertype && crosstalkVo.getTabletype() == tabletype && crosstalkVo.getRoomtype() == roomtype) {
				listS.add(crosstalkVo);
			}
		}
		String content = "";
		if (listS.size() > 0) {
			Random r = new Random();
			int index = r.nextInt(listS.size());
			CrosstalkVo cr = listS.get(index);
			content = cr.getContent();
		}
		
		return content;
		
	}
	
}
